package me.obstsalat.guildera;

import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.player.PlayerItemHeldEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerPickupItemEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.util.Vector;

public class g_war {
	private static guildera plugin;
	
	g_war(guildera pl) {
		plugin = pl;
	}
	
	public boolean onEntityDamagebyEntity(EntityDamageByEntityEvent event) {
		if(!plugin.checkViableWorld(event.getEntity().getWorld())) {
			return false;
		}
		if(event.getEntityType() == EntityType.VILLAGER || event.getEntityType() == EntityType.IRON_GOLEM) {
			for(guild g : plugin.guilds) {
				if(g.isInGuild(event.getEntity().getLocation()) && event.getEntity().getLocation().getWorld().getName().equals(plugin.overworld.getName())) {
					boolean allow = false;
					for(GuildMember gm : g.getMembers()) {
						for(Player plx : plugin.getServer().getOnlinePlayers()) {
							if(plx.getName().equals(gm.getName())) {
								allow = true;
							}
						}
					}
					if(allow) {
						Entity target = event.getDamager();
						if(target.getType() == EntityType.PLAYER && !g.hasMember(((Player)target).getName())) {
							if(g.getMsgCooldown() <= System.currentTimeMillis()) {
								g.announce(((Player) target).getName() + " is attacking your guild!");
								g.setMsgCooldown(System.currentTimeMillis() + 30000);
							}
						}
					} else {
						if(event.getDamager().getType() == EntityType.PLAYER) {
							((Player) event.getDamager()).sendMessage("You can only attack other guilds when someone of this guild is online.");
						}
						event.setDamage(0);
						event.setCancelled(true);
					}
				}
			}
		}
		return event.isCancelled();
	}
	
	public boolean onEntityExplode(EntityExplodeEvent event) {
		if(!plugin.checkViableWorld(event.getEntity().getWorld())) {
			return false;
		}
		if(!plugin.overworld.getName().equals(event.getEntity().getWorld().getName())) {
			return false;
		}
		boolean evil = false;
		for(guild g : plugin.guilds) {
			boolean online;
			online = false;
			for(Player plx : plugin.getServer().getOnlinePlayers()) {
				if(g.hasMember(plx.getName())) {
					online = true;
				}
			}
			boolean affect;
			affect = false;
			for(int i = 0; i < event.blockList().size(); i++) {
				Block bl = event.blockList().get(i);
				if(g.isInGuild(bl.getLocation())) {
					affect = true;
				}
			}
			if(!online && affect) {
				evil = true;
			}
		}
		event.setCancelled(evil);
		return evil;
	}
	
	public boolean onBlockBreak(BlockBreakEvent event) {
		Block bl = event.getBlock();
		if(!plugin.checkViableWorld(bl.getWorld())) {
			return false;
		}
		if(!plugin.overworld.getName().equals(event.getBlock().getWorld().getName())) {
			return false;
		}
		if(!event.getPlayer().isOp()) {
			for(guild g : plugin.guilds) {
				if(g.isInGuild(event.getBlock().getLocation()) && !g.hasMember(event.getPlayer().getName())) {
					boolean evil = true;
					for(Player plx : plugin.getServer().getOnlinePlayers()) {
						if(g.hasMember(plx.getName())) {
							evil = false;
						}
					}
					if(evil) {
						event.setCancelled(true);
						event.getPlayer().sendMessage("You can only attack other guilds when someone of this guild is online.");
					} else {
						if(g.getMsgCooldown() <= System.currentTimeMillis()) {
							g.announce(event.getPlayer().getName() + " is attacking your guild!");
							g.setMsgCooldown(System.currentTimeMillis() + 30000);
						}
					}
				}
			}
			
		}
		return event.isCancelled();
	}
	
	public boolean onBlockPlace(BlockPlaceEvent event) {
		Player ply = event.getPlayer();
		Block bl = event.getBlockPlaced();
		if(!plugin.checkViableWorld(ply.getWorld())) {
			return false;
		}
		if(!ply.isOp())  {
			if(plugin.overworld.getName().equals(ply.getWorld().getName())) {
				boolean b1 = true;
				for(guild g : plugin.guilds) {
					if(g.isInGuild(bl.getLocation()) && !g.hasMember(ply.getName())) {
						b1 = false;
					}
				}
				if(!b1 && bl.getTypeId() != 65) {
					event.setBuild(false);
					ply.sendMessage("You cant place blocks in enemy guilds.");
				}
			}
		}
		return !event.canBuild();
	}
	
	public boolean onPlayerPickupItem(PlayerPickupItemEvent event) {
		if(!plugin.checkViableWorld(event.getPlayer().getWorld())) {
			return false;
		}
		event.setCancelled(true);
		boolean restrict = ItemCheck(event.getPlayer(), ( event.getItem().getItemStack().getTypeId()), true);
		if(restrict) {
			int p = -1;
			for(int p2 = 0; p2 < event.getPlayer().getInventory().getContents().length; p2++) {
				if(event.getPlayer().getInventory().getContents()[p2] == null && p2 != event.getPlayer().getInventory().getHeldItemSlot()) {
					p = p2;
				}
			}
			if(p > -1) {
				event.getItem().remove();
				event.getPlayer().getInventory().setItem(p, event.getItem().getItemStack());
			} else {
				double dir1 = Math.random();
				if(dir1 > 0.5) {
					dir1 = 0.5;
				} else {
					dir1 = -0.5;
				}
				double dir2 = Math.random();
				if(dir2 > 0.5) {
					dir2 = 0.5;
				} else {
					dir2 = -0.5;
				}
				event.getItem().setVelocity(new Vector(Math.random()*dir1,0,Math.random()*dir2));
			}
		} else {
			event.setCancelled(false);
		}
		return event.isCancelled();
	}
	
	public boolean onPlayerJoin (PlayerJoinEvent event) {
		if(!plugin.checkViableWorld(event.getPlayer().getWorld())) {
			return false;
		}
		ToolCheck(event.getPlayer(),-1);
		return false;
	}
	
	public boolean onPlayerItemHeld(PlayerItemHeldEvent event) { 
		if(!plugin.checkViableWorld(event.getPlayer().getWorld())) {
			return false;
		}
		ToolCheck(event.getPlayer(), event.getNewSlot());
		return false;
	}
	
    public boolean onInventoryClose(InventoryCloseEvent event)
    {
		if(!plugin.checkViableWorld(event.getPlayer().getWorld())) {
			return false;
		}
        ToolCheck((Player)event.getPlayer(),-1);
        return false;
    }
	
	private void ToolCheck(Player ply, int slot) {
		if(slot == -1) {
			if(ply.getInventory().getItemInHand() != null) {
					boolean restrict = ItemCheck(ply, ply.getInventory().getItemInHand().getTypeId(), false);
					if(restrict) {
						ply.getWorld().dropItem(ply.getLocation(), ply.getInventory().getItemInHand());
						ply.getInventory().setItem(ply.getInventory().getHeldItemSlot(), new ItemStack(0));
						ply.setItemInHand(new ItemStack(0));
					}
	        	
			}
		} else {
			if(ply.getInventory().getItem(slot) != null) {
				boolean restrict = ItemCheck(ply, ply.getInventory().getItem(slot).getTypeId(), false);
				if(restrict) {
					ply.getWorld().dropItem(ply.getLocation(), ply.getInventory().getItem(slot));
					ply.getInventory().setItem(slot, new ItemStack(0));
					ply.setItemInHand(new ItemStack(0));
				}
        	
		}
		}
	}
	
	private boolean ItemCheck(Player ply, int id, boolean silent) {
		boolean restrict = false;
		try {
			if(ply.isOp()) {
				silent = true;
			}
			for(guild g : plugin.guilds) {
				if(!g.hasMember(ply.getName())) {
					if(g.isInGuild(ply.getLocation())) {
						boolean c = false;
						for(Player p : plugin.getServer().getOnlinePlayers()) {
							if(g.hasMember(p.getName())) {
								c = true;
							}
						}
						if(!c && id != 0 && id != 319 && id != 320 && id != 322 && id != 349 && id != 350 && id != 360 && id != 363 && id != 364 && id != 365 && id != 366 && id != 367 && id != 357 && id != 297 && id != 260) {
							restrict = true;
							if(!silent) {
								ply.sendMessage("You can only attack other guilds when someone of that guild is online.");
							}
						} else if(id != 267 && id != 261 && id != 262 && id != 268 && id != 272 && id != 276 && id != 283 && id != 65 && id != 0 && id != 319 && id != 320 && id != 322 && id != 349 && id != 350 && id != 360 && id != 363 && id != 364 && id != 365 && id != 366 && id != 367 && id != 357 && id != 297 && id != 260) {
							restrict = true;
							if(!silent) {
								ply.sendMessage("You can only use weapons in enemy guilds.");
							}
						}
					}
				}
			}
			
			if(ply.isOp())
			{
				restrict = false;
			}
			
			return restrict;
		} catch (Exception ex) {
			plugin.logger.info("[GuildEra] Can't keep up with server.");
		}
		return restrict;
	}
}
